<?
/**
 * Copyright 2008 Melange.
 *
 * This file is part of Melange CMS
 *
 * @package cms
 *
 */


/**
 * Requirements
 */
require_once("mvc/action/ActionForm.php");
require_once("mvc/action/ActionMessage.php");
require_once("mvc/action/ActionMessages.php");
require_once("cms/content/om/ContentI18n.php");



/**
 * BaseContentForm Class
 *
 * @category    Melange CMS
 * @package     cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 */
class BaseContentForm extends ActionForm {


    // ----------------------------------------------------- Instance Variables


    /**
     * contentI18n
     *
     * @var ContentI18n 
     */
    protected $contentI18n;


    /**
     * day
     *
     * @var mixed 
     */
    protected $day;


    /**
     * month
     *
     * @var mixed 
     */
    protected $month;


    /**
     * year
     *
     * @var mixed 
     */
    protected $year;


    /**
     * formImage
     *
     * @var mixed 
     */
    protected $formImage;


    /**
     * deleteImage
     *
     * @var mixed 
     */
    protected $deleteImage;


    // ----------------------------------------------------------- Constructors


    /**
     * Construct new BaseContentForm
     */
    public function __construct() {
        $this->contentI18n = new ContentI18n();
        $this->day = null;
        $this->month = null;
        $this->year = null;
        $this->formImage = null;
        $this->deleteImage = false;
    }


    // ------------------------------------------------------------- Properties


    /**
     * Get ContentI18n
     *
     * @return ContentI18n 
     */
    public function getContentI18n() {
        return $this->contentI18n;
    }


    /**
     * Set ContentI18n
     *
     * @param ContentI18n  contentI18n
     */
    public function setContentI18n(ContentI18n $contentI18n) {
        $this->contentI18n = $contentI18n;
    }


    /**
     * Get Day
     *
     * @return mixed 
     */
    public function getDay() {
        return $this->day;
    }


    /**
     * Set Day
     *
     * @param mixed  day
     */
    public function setDay($day) {
        $this->day = $day;
    }


    /**
     * Get Month
     *
     * @return mixed 
     */
    public function getMonth() {
        return $this->month;
    }


    /**
     * Set Month
     *
     * @param mixed  month
     */
    public function setMonth($month) {
        $this->month = $month;
    }


    /**
     * Get Year
     *
     * @return mixed 
     */
    public function getYear() {
        return $this->year;
    }


    /**
     * Set Year
     *
     * @param mixed  year
     */
    public function setYear($year) {
        $this->year = $year;
    }


    /**
     * Get FormImage
     *
     * @return mixed 
     */
    public function getFormImage() {
        return $this->formImage;
    }


    /**
     * Set FormImage
     *
     * @param mixed  formImage
     */
    public function setFormImage($formImage) {
        $this->formImage = $formImage;
    }


    /**
     * Get DeleteImage
     *
     * @return mixed 
     */
    public function getDeleteImage() {
        return $this->deleteImage;
    }


    /**
     * Set DeleteImage
     *
     * @param mixed  deleteImage
     */
    public function setDeleteImage($deleteImage) {
        $this->deleteImage = $deleteImage;
    }


    // --------------------------------------------------------- Public Methods


    /**
     * Validate this form
     *
     * @return array $errors
     */
    public function validate() {

        $errors = new ActionMessages();

        if(is_null($this->getContentI18n()->getName()) || strlen($this->getContentI18n()->getName()) == 0)
            $errors->add("contentI18n->name",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.name", true)));


        if(!is_null($this->getContentI18n()->getName()) && strlen($this->getContentI18n()->getName()) > 0 && strlen($this->getContentI18n()->getName()) > 255)
            $errors->add("contentI18n->name",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.name", true),
                    new ActionMessage("255", false))));


        if(is_null($this->getContentI18n()->getBody()) || strlen($this->getContentI18n()->getBody()) == 0)
            $errors->add("contentI18n->body",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.body", true)));


        if(!is_null($this->getContentI18n()->getContent()->getLink()) && strlen($this->getContentI18n()->getContent()->getLink()) > 0 && !preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $this->getContentI18n()->getContent()->getLink()))
            $errors->add("contentI18n->content->link",
                new ActionMessage("errors.url", true, new ActionMessage("prompt.link", true)));


        if(!is_null($this->getContentI18n()->getContent()->getLink2()) && strlen($this->getContentI18n()->getContent()->getLink2()) > 0 && !preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $this->getContentI18n()->getContent()->getLink2()))
            $errors->add("contentI18n->content->link2",
                new ActionMessage("errors.url", true, new ActionMessage("prompt.link2", true)));


        global $ALLOWABLE_IMAGES;
        if(!is_null($this->getFormImage()) && isset($this->formImage["size"]) && $this->formImage["size"] > 0 && !array_key_exists($this->formImage["type"], $ALLOWABLE_IMAGES))
            $errors->add("formImage",
                new ActionMessage("errors.image", true, new ActionMessage("prompt.image", true)));

        return $errors;
    }

}
?>